package vues;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;

import sourceCode.Aeroport;
import sourceCode.Chariot;

public class VueChariot extends Vue {

    private Chariot chariot;
    private VueAeroport vueAeroport;

    private Color couleurNormal = new Color(51, 102, 255);
    private Color couleurBagage = new Color(0, 0, 250);
    private Color couleurSelectionne = new Color(0, 130, 250);
    private Color couleurAttenteChoix = new Color(250, 0, 250);

    //Constantes
    private int hauteurChariot = 15;
    private int longueurChariot = 15;
    private int hauteurEtincelle = hauteurChariot / 4;
    private Rectangle2D.Double rectangleChariot;
    private Rectangle2D.Double rectangleEtincelle;

    public VueChariot(VueAeroport v, Chariot c) {
        super();
        this.chariot = c;
        this.vueAeroport = v;
        Point centre = this.vueAeroport.convertCoordonnesToPixels(c.getCoordonnees());
        this.rectangleChariot = setRectangleChariot(centre);
        this.rectangleEtincelle = setRectangleEtincelle(centre);
    }

    public boolean estCliquee(int x, int y) {
        return this.rectangleChariot.contains(x, y);
    }

    public Rectangle2D.Double setRectangleChariot(Point centre) {
        return new Rectangle2D.Double(centre.getX() - this.longueurChariot / 2, centre.getY() - this.hauteurChariot / 2,
                                      this.longueurChariot, this.hauteurChariot);
    }

    public Rectangle2D.Double setRectangleEtincelle(Point centre) {
        return new Rectangle2D.Double(centre.getX() - this.longueurChariot / 2, centre.getY() + this.hauteurChariot / 4,
                                      this.longueurChariot, this.hauteurEtincelle);
    }

    @Override
    public void dessiner(Graphics g) {
        if (chariot.getTypePosition() != Chariot.typePosition.garage) {
            Color cTemp = g.getColor();

            if (chariot.getBagage() != null) {
                g.setColor(this.couleurBagage);
            } else {
                g.setColor(this.couleurNormal);
            }

            Point centre = vueAeroport.convertCoordonnesToPixels(this.chariot.getCoordonnees());
            this.rectangleChariot = setRectangleChariot(centre);
            this.rectangleEtincelle = setRectangleEtincelle(centre);
            g.fillRect((int)this.rectangleChariot.x, (int)this.rectangleChariot.y, (int)this.rectangleChariot.width,
                       (int)this.rectangleChariot.height);

            if (chariot.getFreinage() == 0) {
                g.setColor(this.couleurNormal);
            } else {
                int coefficientFreinage = (int)this.chariot.getFreinage() * 2;
                int VBFreinage = 0;
                if (coefficientFreinage == 0)
                    VBFreinage = 201;
                else if ((255 / coefficientFreinage) > 201)
                    VBFreinage = 201;
                else
                    VBFreinage = (int)Math.round(255 / coefficientFreinage);
                g.setColor(new Color(255, VBFreinage, VBFreinage));
            }
            g.fillRect((int)this.rectangleEtincelle.x, (int)this.rectangleEtincelle.y, (int)this.rectangleEtincelle.width,
                       (int)this.rectangleEtincelle.height);

            if (estSelectionnee()) {
                g.setColor(this.couleurSelectionne);
                Graphics2D g2d = (Graphics2D)g;
                float dash1[] = { 3.5f };
                Stroke rectStroke =
                    new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, dash1, 0.0f);
                g2d.setStroke(rectStroke);
                g2d.drawRect((int)this.rectangleChariot.x, (int)this.rectangleChariot.y, (int)this.rectangleChariot.width,
                             (int)this.rectangleChariot.height);
                g2d.setStroke(new BasicStroke()); //on restaure le style par d�faut
            }
            if (!Aeroport.getPilotageAutomatique() &&
                chariot.getNoeud() != null) { //si on est en mode manuel et que le chariot est � un noeud
                g.setColor(this.couleurAttenteChoix);
                Graphics2D g2d = (Graphics2D)g;
                float dash1[] = { 3.5f };
                Stroke rectStroke =
                    new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10f, dash1, 0.0f);
                g2d.setStroke(rectStroke);
                g2d.drawRect((int)this.rectangleChariot.x, (int)this.rectangleChariot.y, (int)this.rectangleChariot.width,
                             (int)this.rectangleChariot.height);
            }

            g.setColor(cTemp);
        }
        return;
    }

    @Override
    public String getType() {
        return "VueChariot";
    }

    public Chariot getChariot() {
        return this.chariot;
    }
}
